﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Super_Wall_Breaker.Sources.GameElements
{
    /// <summary>
    /// Sprite is a class used to draw texture2D
    /// </summary>
    public class Sprite
    {
        private Vector2 _position;
        private Texture2D _texture;
        private Rectangle? _hitbox = null;
        private Rectangle? _sourceRectangle = null;
        private Color _color = Color.White;
        private float _rotation = 0; //RAD
        private Vector2 _origin = Vector2.Zero;
        private Vector2 _scale = Vector2.One;
        private SpriteEffects _effect = SpriteEffects.None;
        private float _layerDepth = 0;

        /// <summary>
        /// Gets or sets the source rectangle.
        /// </summary>
        /// <value>
        /// The source rectangle.
        /// </value>
        public Rectangle? SourceRectangle
        {
            get { return this._sourceRectangle; }
            set { this._sourceRectangle = value; }
        }

        /// <summary>
        /// Gets or sets the position.
        /// </summary>
        /// <value>
        /// The position.
        /// </value>
        public Vector2 Position
        {
            get { return this._position; }
            set { this._position = value; }
        }

        /// <summary>
        /// Gets the texture.
        /// </summary>
        public Texture2D Texture
        {
            get { return _texture; }
        }

        /// <summary>
        /// Gets or sets the hitbox.
        /// </summary>
        /// <value>
        /// The hitbox.
        /// </value>
        public Rectangle? Hitbox
        {
            get
            {
                Rectangle hitbox;

                if (this._hitbox == null)
                {
                    if (this._sourceRectangle == null)
                    {
                        hitbox = new Rectangle((int)this.Position.X - (int)this.Origin.X, (int)this.Position.Y - (int)this.Origin.Y, this.Texture.Width, this.Texture.Height);
                    }
                    else
                    {
                        hitbox = new Rectangle((int)this.Position.X - (int)this.Origin.X, (int)this.Position.Y - (int)this.Origin.Y, this._sourceRectangle.Value.Width, this._sourceRectangle.Value.Height);
                    }
                }
                else
                {
                    hitbox = (Rectangle)this._hitbox;
                }

                return hitbox;
            }
            set { this._hitbox = value; }
        }

        /// <summary>
        /// Gets or sets the color.
        /// </summary>
        /// <value>
        /// The color.
        /// </value>
        public Color Color
        {
            get { return this._color; }
            set { this._color = value; }
        }

        /// <summary>
        /// Gets or sets the rotation.
        /// </summary>
        /// <value>
        /// The rotation.
        /// </value>
        public float Rotation
        {
            get { return this._rotation; }
            set { this._rotation = value; }
        }

        /// <summary>
        /// Gets or sets the origin.
        /// </summary>
        /// <value>
        /// The origin.
        /// </value>
        public Vector2 Origin
        {
            get { return this._origin; }
            set { this._origin = value; }
        }

        /// <summary>
        /// Gets or sets the scale.
        /// </summary>
        /// <value>
        /// The scale.
        /// </value>
        public Vector2 Scale
        {
            get { return this._scale; }
            set { this._scale = value; }
        }

        /// <summary>
        /// Gets or sets the effect.
        /// </summary>
        /// <value>
        /// The effect.
        /// </value>
        public SpriteEffects Effect
        {
            get { return this._effect; }
            set { this._effect = value; }
        }

        /// <summary>
        /// Gets or sets the layer depth.
        /// </summary>
        /// <value>
        /// The layer depth.
        /// </value>
        public float LayerDepth
        {
            get { return this._layerDepth; }
            set { this._layerDepth = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Sprite"/> class.
        /// </summary>
        public Sprite()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Sprite"/> class.
        /// </summary>
        /// <param name="position">The position of the sprite.</param>
        public Sprite(Vector2 position)
        {
            this._position = position;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Sprite"/> class.
        /// </summary>
        /// <param name="position">The position of the sprite.</param>
        /// <param name="sourceRectangle">The source rectangle.</param>
        public Sprite(Vector2 position, Rectangle? sourceRectangle)
        {
            this.Position = position;
            this.SourceRectangle = sourceRectangle;
        }

        /// <summary>
        /// Loads the Texture2D in the game.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <param name="assetName">Name of the asset.</param>
        public void LoadContent(ContentManager content, string assetName)
        {
            _texture = content.Load<Texture2D>(assetName);
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, _position, _sourceRectangle, _color, _rotation, _origin, _scale, _effect, _layerDepth);
        }
    }
}
